package com.harmonk.firstscene.ui.model

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.harmonk.firstscene.FirstSceneApplication
import com.harmonk.firstscene.db.entity.Person
import kotlinx.coroutines.launch

class PersonViewModel(application: Application) : AndroidViewModel(application) {
    private val personRepository = (application as FirstSceneApplication).personRepository
    
    private val _persons = MutableLiveData<List<Person>>()
    val persons: LiveData<List<Person>> = _persons
    
    private val _currentPerson = MutableLiveData<Person>()
    val currentPerson: LiveData<Person> = _currentPerson
    
    fun loadPersonsByEventId(eventId: Long) {
        viewModelScope.launch {
            try {
                personRepository.getPersonsByEventId(eventId).collect { personList: List<Person> ->
                    _persons.postValue(personList)
                }
            } catch (e: Exception) {
                // Handle exception
                _persons.postValue(emptyList())
            }
        }
    }
    
    fun loadPersonsByVehicleId(vehicleId: Long) {
        viewModelScope.launch {
            try {
                personRepository.getPersonsByVehicleId(vehicleId).collect { personList: List<Person> ->
                    _persons.postValue(personList)
                }
            } catch (e: Exception) {
                // Handle exception
                _persons.postValue(emptyList())
            }
        }
    }
    
    fun getPersonById(id: Long) {
        viewModelScope.launch {
            try {
                val person = personRepository.getPersonById(id)
                person?.let { p: Person ->
                    _currentPerson.postValue(p)
                }
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
    
    fun insertPerson(person: Person): Long {
        var id: Long = -1
        viewModelScope.launch {
            try {
                id = personRepository.insertPerson(person)
            } catch (e: Exception) {
                // Handle exception
            }
        }
        return id
    }
    
    fun updatePerson(person: Person) {
        viewModelScope.launch {
            try {
                personRepository.updatePerson(person)
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
    
    fun deletePerson(person: Person) {
        viewModelScope.launch {
            try {
                personRepository.deletePerson(person)
            } catch (e: Exception) {
                // Handle exception
            }
        }
    }
}

class PersonViewModelFactory(private val application: Application) : ViewModelProvider.Factory {
    override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(PersonViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return PersonViewModel(application) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}